package com.hibaidubackup.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 获取博客分类，生成对应的目录
 * @author zheng
 *
 */
public class GetCatalogList {
	
	public static void main(String[] args) {
		String url = BackupBlog.hibaidubackupURL;
		List<HashMap<String,String>> list = GetCatalogList.getCatalogList(url);
		System.out.println("\n CatalogList====================\n ");
		System.out.println(list);
		GetCatalogList.createCatalogDir(list);
	}
	
	/**
	 * 创建目录
	 * @param menu
	 */
	public static void createCatalogDir(List<HashMap<String,String>> list){
		for(HashMap<String,String> catalog : list){
			String cataURL = catalog.get("cataURL");
			String cataName = catalog.get("cataName");
			String cataCount = catalog.get("cataCount");
			FileUtil.createDir(cataName);
		}
	}
	
	/**
	 * 把body里面的内容变成一行，便于分析判断
	 * @param urlname
	 * @return
	 */
	public static List<HashMap<String,String>> getCatalogList(String url) {
		String catalogStr = GetCatalogList.getCatalogStr(url);
		System.out.println("\n catalogStr====================\n ");
		System.out.println(catalogStr);
		FileUtil.createHtml(catalogStr, "zdz8207", BackupBlog.hibaidubackupPath+"catalog.html");
		
		List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
		
		String[] items = catalogStr.split("<div class=\"item\"><a href=\"");
		//items[0]为空 从1开始
		for(int i=1; i<items.length; i++){
			///zdz8207/blog/category/%C4%AC%C8%CF%B7%D6%C0%E0" title="查看该分类中所有文章">默认分类</a>(52)</div><div class="line">&nbsp;</div>
			String item = items[i];
			//System.out.println("\n item====================\n ");
			//System.out.println(item);
			String[] cata = item.split("\" title=\"查看该分类中所有文章\">");
			String cataURL = cata[0];
			String cataName = cata[1].split("</a>\\(")[0];
			String cataCount = cata[1].split("</a>\\(")[1].split("\\)")[0];
			//map 必须在这里new 否则最终都是保存最后一个map对象
			HashMap<String,String> map = new HashMap<String,String>();
			map.put("cataURL", cataURL);
			map.put("cataName", cataName);
			map.put("cataCount", cataCount);
			list.add(map);
		}
		
		return list;
	}
	
	/**
	 * 把body里面的内容变成一行，便于分析判断
	 * @param urlname
	 * @return
	 */
	public static String getCatalogStr(String url) {
		String bodyStr = GetURLContent.getBodyStr(url);
		//System.out.println("\n bodyStr====================\n ");
		//System.out.println(bodyStr);
		String start = "<div id=\"m_artclg\" class=\"modbox\">";
		String end = "<div class=\"line\">&nbsp;</div></div>";
		if ("".equals(bodyStr)) {
			return "";
		}
		String str = "";
		try {
			str = bodyStr.split(start)[1];
			str = str.split(end)[0];
		} catch (Exception e) {
			return "";
		}
		return str;
	}

}

